﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.Commons.Enums
{
    /// <summary>
    /// 设备部件枚举
    /// </summary>
    public enum DeviceComponentTypeEnum : int
    {
        [Description("未知部件")]
        All = 0,
        [Description("炉管")]
        Tube = 1,
        [Description("机械手")]
        J = 2,


    }

    public static class DeviceComponentEnumExtension
    {
        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this DeviceComponentTypeEnum value)
        {
            var field = value.GetType().GetField(value.ToString());
            var attribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
            return attribute?.Description ?? value.ToString();
        }

        public static DeviceComponentTypeEnum GetEnumByDescription(string description)
        {
            foreach (var value in Enum.GetValues(typeof(DeviceComponentTypeEnum)).Cast<DeviceComponentTypeEnum>())
            {
                if (value.GetDescription() == description)
                {
                    return value;
                }
            }
            throw new ArgumentException($"No enum found with description '{description}'");
        }

        public static DeviceComponentTypeEnum GetEnumByValue(int value)
        {
            if (Enum.IsDefined(typeof(DeviceComponentTypeEnum), value))
            {
                return (DeviceComponentTypeEnum)value;
            }
            throw new ArgumentException($"No enum found with value '{value}'");
        }

        public static List<DeviceComponentTypeEnum> GetAllEnums()
        {
            return Enum.GetValues(typeof(DeviceComponentTypeEnum)).Cast<DeviceComponentTypeEnum>().ToList();
        }
        public static List<string> GetAllDescriptions()
        {
            return GetAllEnums().Select(e => e.GetDescription()).ToList();
        }
        public static List<int> GetAllValues()
        {
            return GetAllEnums().Select(e => (int)e).ToList();
        }
    }
}
